package com.cc.pms.service.impl;

import com.cc.pms.domain.TeamTag;
import com.cc.pms.domain.vo.TeamTagVo;
import com.cc.pms.mapper.TeamTagMapper;
import com.cc.common.core.domain.R;
import com.cc.pms.service.ITeamTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 关联队伍与标签，记录队伍的标签信息 服务实现类
 * </p>
 *
 * @author corleone
 * @since 2025-04-19
 */
@Service
public class TeamTagServiceImpl extends ServiceImpl<TeamTagMapper, TeamTag> implements ITeamTagService {


    @Override
    public R<Boolean> updateTags(Long teamId, Long[] tags) {
        removeByTeamId(teamId);
        if (tags == null || tags.length == 0){
            return R.ok();
        }
        List<TeamTag> teamTagList = Arrays.stream(tags).map(tagId -> {
            TeamTag teamTag = new TeamTag();
            teamTag.setTagId(tagId);
            teamTag.setTeamId(teamId);
            return teamTag;
        }).collect(Collectors.toList());
        return R.ok(saveBatch(teamTagList));
    }

    @Override
    public List<TeamTagVo> queryTeamTagVoByTeamId(Long teamId) {
        return baseMapper.queryTeamTagVoByTeamId(teamId);
    }

    @Override
    public void removeByTagIds(Long[] ids) {
        if (ids == null || ids.length == 0){
            return;
        }
        lambdaUpdate().in(TeamTag::getTagId , Arrays.stream(ids).toArray()).remove();
    }

    @Override
    public void removeByTeamId(Long teamId) {
        lambdaUpdate().eq(TeamTag::getTeamId , teamId).remove();
    }
}
